package com.bankcomm.bean;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * 票牌基类
 * @author huang.x.h
 *
 * @date 2012-5-24 下午07:11:53
 */
public class Ticket {
	public static final int TRAIN_NUM = 0;
	public static final int AIR_NUM = 1;
	public static final int MOVIE_NUM = 2;
	public static final int LOTTERY_NUM = 3;
	
	/** 属性列表 */
	public final String KEY_TICKET_TYPE = "ticketType";
	public final String KEY_PROPERTIES = "props";
	
	/** 默认票据类型 */
	public String TICKET_TYPE = "train";
	
	
	public String getTicketType() {
		return TICKET_TYPE;
	}
	
	public void setTicketType(String type) {
		TICKET_TYPE = type;
	}
	
	/**
	 * 解析票据( train:火车票; air:飞机票; movie:电影票; lottery:彩票; air_booking: 机票预定;)
	 */
	public ArrayList<Ticket> parse(String jsonReply, int num) {
		// 保存所有的票
		ArrayList<Ticket> allTickets = null;
		
		// 解析
		if(jsonReply != null) {
			try {
				allTickets = new ArrayList<Ticket>();

				JSONArray objTicket = new JSONArray((String)jsonReply);
				JSONObject singTicket = null;

				if (num == TRAIN_NUM) {
					for (int i = 0; i < objTicket.length(); i++) {
						singTicket = (JSONObject) objTicket.get(i);
						allTickets.add(parseTrainTicket(singTicket));
					}
				} else if (num == AIR_NUM) {
					for (int i = 0; i < objTicket.length(); i++) {
						singTicket = (JSONObject) objTicket.get(i);
						allTickets.add(parseAirTicket(singTicket));
					}
				} 

				/*JSONObject objTicket = new JSONObject(jsonReply);
				setTicketType(objTicket.getString(KEY_TICKET_TYPE));
				
				JSONArray arrTicket = new JSONArray(objTicket.getString(KEY_PROPERTIES));
				JSONObject singTicket = null;
				final int arrSize = arrTicket.length();
				for(int i = 0; i < arrSize; i++) {
					singTicket = arrTicket.getJSONObject(i);
					if(TICKET_TYPE.equals("train")) {
						allTickets.add(parseTrainTicket(singTicket));
					} else if (TICKET_TYPE.equals("air")) {
						allTickets.add(parseAirTicket(singTicket));
					} else if (TICKET_TYPE.equals("movie")) {
//						allTickets.add(parseMovieTicket(singTicket));
					} else if (TICKET_TYPE.equals("lottery")) {
//						allTickets.add(parseLotteryTicket(singTicket));
					} else if (TICKET_TYPE.equals("air_booking")) {
//						allTickets.add(parseAirTicketBooking(singTicket));
					}
				}*/
				
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
		
		return allTickets;
	}
	
	/**
	 * 解析火车票
	 * @param train
	 */
	public TrainTicket parseTrainTicket(JSONObject train) {
		TrainTicket ticket = null;
		try {
			ticket = new TrainTicket();
			ticket.setOrg(train.getString(TrainTicket.KEY_ORG));
			ticket.setDst(train.getString(TrainTicket.KEY_DST));
			ticket.setNo(train.getString(TrainTicket.KEY_NO));
			ticket.setType(train.getString(TrainTicket.KEY_TYPE));
			ticket.setDepartDate(train.getString(TrainTicket.KEY_DEPART_DATE));
			
			ticket.setArriveDate(train.getString(TrainTicket.KEY_ARRIVE_DATE));
			ticket.setDistance(train.getString(TrainTicket.KEY_DISTANCE));
			ticket.setTime(train.getString(TrainTicket.KEY_TIME));
			ticket.setTicket_min_price(train.getString(TrainTicket.KEY_MIN_PRICE));
			ticket.setTicket_max_price(train.getString(TrainTicket.KEY_MAX_PRICE));
			ticket.setUrl(train.getString(AirTicket.KEY_URL));
			
//			ticket.setPrice(train.getString(TrainTicket.KEY_PRICE));
//			ticket.setIdCard(train.getString(TrainTicket.KEY_IDCARD));
//			ticket.setPosition(train.getString(TrainTicket.KEY_POSITION));
//			ticket.setPassenger(train.getString(TrainTicket.KEY_PASSENGER));
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return ticket;
	}
	
	/**
	 * 解析飞机票
	 * @param air
	 */
	public AirTicket parseAirTicket(JSONObject air) {
		AirTicket ticket = null;
		try {
			ticket = new AirTicket();
			ticket.setStartAirport(air.getString(AirTicket.KEY_START_AIRPORT));
			ticket.setEndAirport(air.getString(AirTicket.KEY_END_AIRPORT));
			ticket.setIconvoyage(air.getString(AirTicket.KEY_ICON_VOYAGE));
			ticket.setVoyageName(air.getString(AirTicket.KEY_NAME_VOYAGE));
			ticket.setNumAndSeat(air.getString(AirTicket.KEY_NUM_AND_SEAT));
			ticket.setOrg(air.getString(AirTicket.KEY_ORG));
			ticket.setDst(air.getString(AirTicket.KEY_DST));
			ticket.setAmount(air.getString(AirTicket.KEY_AMOUNT));
			ticket.setShipType(air.getString(AirTicket.KEY_SHIT_TYPE));
			ticket.setShipPrice(air.getString(AirTicket.KEY_SHIT_PRICE));
			ticket.setUrl(air.getString(AirTicket.KEY_URL));
			
			
			/*	{
			"startAirport":"11:20",
			"endAirport":"20:20",
			"iconvoyage":"boc_cj.png",
			"voyageName":"西航",
			"numAndSeat":"CZ3108 A",
			"org":"上海虹桥",
			"dst":"北京首都",
			"amount":"$100",
			"shipType":"全价仓",
			"shipPrice":"全价舱",
			"url":"http://192.168.1.1/nidaye"
			}*/
			
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return ticket;
	}
	
	/**
	 * 解析电影票
	 * @param air
	 */
	/*public MovieTicket parseMovieTicket(JSONObject movie) {
		MovieTicket ticket = null;
		try {
			ticket = new MovieTicket();
			ticket.setCinema(movie.getString(MovieTicket.KEY_CINEMA));
			ticket.setDate(movie.getString(MovieTicket.KEY_DATE));
			ticket.setTime(movie.getString(MovieTicket.KEY_TIME));
			ticket.setHall(movie.getString(MovieTicket.KEY_HALL));
			ticket.setSeat(movie.getString(MovieTicket.KEY_SEAT));
			ticket.setPrice(movie.getString(MovieTicket.KEY_PRICE));
			ticket.setImg(movie.getString(MovieTicket.KEY_IMG));
			
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return ticket;
	}
	*/
	/**
	 * 解析彩票
	 * @param lottery
	 */
	/*public LotteryTicket parseLotteryTicket(JSONObject lottery) {
		LotteryTicket ticket = null;
		try {
			ticket = new LotteryTicket();
			ticket.setMachineNo(lottery.getString(LotteryTicket.KEY_MACHINE_NO));
			ticket.setType(lottery.getString(LotteryTicket.KEY_TYPE));
			ticket.setIssue(lottery.getString(LotteryTicket.KEY_ISSUE));
			ticket.setSerial(lottery.getString(LotteryTicket.KEY_SERIAL));
			ticket.setPeriod(lottery.getString(LotteryTicket.KEY_PERIOD));
			ticket.setAmount(lottery.getString(LotteryTicket.KEY_AMOUNT));
			ticket.setDraw(lottery.getString(LotteryTicket.KEY_DRAW));
			ticket.setNumbers(lottery.getString(LotteryTicket.KEY_NUMBERS));
			
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return ticket;
	}
	*/
	/**
	 * 解析飞机票
	 * @param air
	 */
	/*public AirTicketBook parseAirTicketBooking(JSONObject air) {
		AirTicketBook ticket = null;
		try {
			ticket = new AirTicketBook();
			ticket.setTripType(air.getString(AirTicketBook.KEY_TRIP_TYPE));
			ticket.setNo(air.getString(AirTicketBook.KEY_NO));
			ticket.setType(air.getString(AirTicketBook.KEY_TYPE));
			ticket.setCabin(air.getString(AirTicketBook.KEY_CABIN));
			ticket.setDepartDate(air.getString(AirTicketBook.KEY_DEPART_DATE));
			ticket.setOrg(air.getString(AirTicketBook.KEY_ORG));
			ticket.setDst(air.getString(AirTicketBook.KEY_DST));
			ticket.setPrice(air.getString(AirTicketBook.KEY_PRICE));
			ticket.setButtonText(air.getString(AirTicketBook.KEY_BUTTON_TEXT));
//			ticket.setButtonUrl(air.getString(AirTicketBook.KEY_BUTTON_URL));
			
			
//			 "props":[{		
//					"tripType":"单程",
//					"no":"国航 CA992",
//					"type":"A330",
//					"cabin":"经济舱",
//					departDate":"2011\12\13",
//					"org":"首都国际机场 10：00am",
//					"dst":"上海虹桥机场 12：00am",
//					"price":"1710",
//					"bookText":"预定"
			
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return ticket;
	}
*/
}
